
import UserData from '../UserData'
var COMMON_VALUE = require('Common')

cc.Class({
    extends: cc.Component,

    properties: {
        cost:Number,
        level:1,
        //gain：//LINEAR
        gainBaseNumber:0,
        gainMultiply:1,
        gainMultiplierList:{//[[等级，倍数],[等级，倍数],[等级，倍数]...]
            default:[],
            type:Array,
        },
        //cost //EXP： PerLevel = costMultiply * costBaseNumber ^ level ;
        costBaseNumber:1.07,       
        costMultiply:1,
    },
//收益
    getCurrentGain(level){
        if(!level){ 
            level = this.level 
        }
        if(level ===0 ){ 
            level = 1
        }
        let preMultiplyList = []
        if(this.gainMultiplierList){
            for(var i=0;i<this.gainMultiplierList.length;i++){
                if(this.gainMultiplierList[i][0]<=level){
                    preMultiplyList.push(this.gainMultiplierList[i][1])
                }
            }
            let gain =this.gainBaseNumber +  this.gainMultiply * (level)
            for(var i=0;i<preMultiplyList.length;i++){
                gain = gain * preMultiplyList[i]
            }
            return gain
        }else{
            return 0
        }

    },
    getLevelUpGain(levelNum) {
        if(levelNum>0){
            return this.getCurrentGain(this.level +levelNum) - this.getCurrentGain(this.level)
        }else if (levelNum === 0){
            return this.getCurrentGain(this.level +1) - this.getCurrentGain(this.level)
        }
    },

//消耗
    getLevelUpCost(levelNum,currentResource){ //返回升levelNum级所需要的消耗 输入-1则返回升级最大等级的消耗和等级
        if(levelNum===0){
            return [0,0]
        }else if(levelNum<0){//最大模式 b^m<(currentResource)*(b-1)/(a*b^n) +1<b^(m+1)
            let tmp = (currentResource)*(this.costBaseNumber-1)/(this.costMultiply*Math.pow(this.costBaseNumber,this.level)) +1
            let maxLevelUp = Math.floor(Math.log(tmp)/Math.log(this.costBaseNumber))        
            let cost = this.costMultiply * Math.pow(this.costBaseNumber,this.level) * (1-Math.pow(this.costBaseNumber,maxLevelUp))/(1-this.costBaseNumber)
            return [cost,maxLevelUp]
        }
        else{
            return [this.costMultiply * Math.pow(this.costBaseNumber,this.level) * (1-Math.pow(this.costBaseNumber,levelNum))/(1-this.costBaseNumber) ,levelNum]
        }
    },

    initManager(){
        this.level = 1
        this.loadJumpArrayData([[10,2],[20,2],[30,2],[40,2],[50,2],[75,2],[100,2],[125,2],[150,2],[175,2],[200,2],[250,2],[300,2],[350,2],[400,2],[450,2],[500,2],[550,2],[600,2],[650,2],[700,2],[800,2],[900,2],[1000,2],[1100,2],[1200,2],[1300,2],[1400,2],[1500,2],[1600,2],[1700,2],[1800,2],[1900,2],[2000,2],[2200,2],[2400,2],[2600,2],[2800,2],[3000,2],[3500,2],[4000,2],[4500,2],[5000,2],[5500,2],[6000,2],[6500,2],[7000,2],[8000,2],[9000,2],[9999,2]])
    },
    loadJumpArrayData(jumpArray){ // e.g. jumpArray = [cc.v2(20,3),cc.v2(50,6),cc.v2(70,3),cc.v2(100,10),cc.v2(130,2),cc.v2(140,10)]
        this.gainMultiplierList = jumpArray
    },
    upgrade (levelNum) {//关联其他脚本的升级事件。返回[是否升级成功,升级所需消耗,]
        this.level += levelNum
    },
    calcUpgrade(levelNum,totalResource) {//返回[升级消耗，级数，是否可以升级]
        let costResult = this.getLevelUpCost(levelNum,totalResource)
        if(costResult[1] === 0){
            return [costResult[0],costResult[1],false]
        }else{
            if(totalResource >=costResult[0]){
                return [costResult[0],costResult[1],true]
            }else{
                return [costResult[0],costResult[1],false]
            }
        }
    },

    start () {
        let floor = 0
        for(let i=0;i<this.node.parent.children.length;i++){
            if(this.node  === this.node.parent.children[i]){
                floor = i+1
            }
        }
        //console.log('UpgradeManager.start',floor)
        if(this.node.getComponent('ResSetup')){
            if(this.node.getComponent('ResSetup').resSetupStarted){
                this.node.getComponent('ResSetup').start()
            }
        }
        


        if(this.gainMultiplierList===[]){
            this.initManager()
        }
        if(!this.gainMultiplierList){
            this.gainMultiplierList = COMMON_VALUE.RESTAURANT_GAIN_MULTIPLY_LIST[0]
        }
        
    },

    // update (dt) {},
});
